<?php
namespace app\index\controller;

use app\index\controller\Base;
use think\Db;
use think\Session;

class Goods extends Base
{
    /**
     * 分类
     * @return mixed
     */
    public function ClassList(){
        //会员信息
        $UserInfo = UserInfo(['id'=>Session::get('user_id')]);
        $this->assign('UserInfo',$UserInfo);
        //分类信息
        $ClassList = Db::name('goods_class')->where(['son'=>0])->select();
        foreach ($ClassList as $k=>$v){
            $ClassList[$k]['son'] = Db::name('goods_class')->where(['parent_id'=>$ClassList[$k]['id']])->select();
        }
        $this->assign('ClassList',$ClassList);
        return $this->fetch('ClassList');
    }

    /**
     * 商品列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GoodsList(){
        //分类ID
        $ClassID = Param('ClassID');
        $this->assign('ClassID',$ClassID);
        //分类信息
        $ClassList = Db::name('goods_class')->where(['son'=>0])->select();
        $this->assign('ClassList',$ClassList);
        //商品列表
        // $GoodsList = Db::name('goods')->where(['is_shelf'=>2,'goods_type'=>1,'parent_id'=>$ClassID])->order('add_time desc')->select();
        $GoodsList = Db::name('goods')->where(['is_shelf'=>2,'goods_type'=>1,'parent_id'=>$ClassID])->order('xu asc')->select();
        $this->assign('GoodsList',$GoodsList);
        return $this->fetch('GoodsList');
    }

    //搜索商品
    public function GoodsSearch(){
        //商品名称
        $GoodsName = Param('goodsname');
        $this->assign('GoodsName',$GoodsName);

        $Where['name'] = array('like','%'.$GoodsName.'%');
        $Where['is_shelf'] = 2;
        $Where['is_invest'] = 0;
        // $GoodsList = Db::name('goods')->where($Where)->order('add_time desc')->select();
        $GoodsList = Db::name('goods')->where($Where)->order('xu asc')->select();
        $this->assign('GoodsList',$GoodsList);

        //轮播图
        $BannerList = Db::name('banner')->where(['place'=>3])->order('xu asc')->select();
        $this->assign('BannerList',$BannerList);

        return $this->fetch('GoodsSearch');
    }

    /**
     * 投资产品
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GoodsInvest(){
        //商品名称
        $GoodsName = Param('goodsname');
        $this->assign('GoodsName',$GoodsName);

        if(!empty($GoodsName)){
            $Where['name'] = array('like','%'.$GoodsName.'%');
        }

        $Where['is_shelf'] = 2;
        $Where['goods_type'] = 4;
        // $GoodsList = Db::name('goods')->where($Where)->order('add_time desc')->select();
        $GoodsList = Db::name('goods')->where($Where)->order('xu asc')->select();
        //倒计时
        $aa = [];
        foreach ($GoodsList as $v){
            if($v['etime'] == ''){
                $v['time'] = 0;
                $aa[] = $v;
            }else{
                // $v['time'] = date('Y-m-d',$v['etime']);
                $v['time'] = $v['etime'];
                $aa[] = $v;
            }
        }
        $this->assign('GoodsList',$aa);
        
        $nowtime = time();
        $this->assign('nowtime',$nowtime);

        //轮播图
        $BannerList = Db::name('banner')->where(['place'=>4])->order('xu asc')->select();
        $this->assign('BannerList',$BannerList);

        return $this->fetch("GoodsInvest");
    }
    
    
    
    public function GoodsInvest1(){
        //会员信息
        $UserInfo = UserInfo(['id'=>Session::get('user_id')]);
        $this->assign('UserInfo',$UserInfo);
        //商品名称
        $GoodsName = Param('goodsname');
        $this->assign('GoodsName',$GoodsName);

        if(!empty($GoodsName)){
            $Where['name'] = array('like','%'.$GoodsName.'%');
        }

        $Where['is_shelf'] = 2;
        $Where['goods_type'] = 3;
        // $GoodsList = Db::name('goods')->where($Where)->order('add_time desc')->select();
        $GoodsList = Db::name('goods')->where($Where)->order('xu asc')->select();
        $this->assign('GoodsList',$GoodsList);
// var_dump($GoodsList);die;
        //轮播图
        $BannerList = Db::name('banner')->where(['place'=>4])->order('xu asc')->select();
        $this->assign('BannerList',$BannerList);

        return $this->fetch("GoodsInvest1");
    }
    
    public function GoodsInvest2(){
        //商品名称
        $GoodsName = Param('goodsname');
        $this->assign('GoodsName',$GoodsName);

        if(!empty($GoodsName)){
            $Where['name'] = array('like','%'.$GoodsName.'%');
        }

        $Where['is_shelf'] = 2;
        $Where['goods_type'] = 2;
        // $GoodsList = Db::name('goods')->where($Where)->order('add_time desc')->select();
        $GoodsList = Db::name('goods')->where($Where)->order('xu asc')->select();
        $this->assign('GoodsList',$GoodsList);

        //轮播图
        $BannerList = Db::name('banner')->where(['place'=>4])->order('xu asc')->select();
        $this->assign('BannerList',$BannerList);

        return $this->fetch("GoodsInvest2");
    }
    
    public function GoodsInvest3(){
        //商品名称
        $GoodsName = Param('goodsname');
        $this->assign('GoodsName',$GoodsName);

        if(!empty($GoodsName)){
            $Where['name'] = array('like','%'.$GoodsName.'%');
        }

        $Where['is_shelf'] = 2;
        $Where['goods_type'] = 1;
        // $GoodsList = Db::name('goods')->where($Where)->order('add_time desc')->select();
        $GoodsList = Db::name('goods')->where($Where)->order('xu asc')->select();
        $this->assign('GoodsList',$GoodsList);

        //轮播图
        $BannerList = Db::name('banner')->where(['place'=>4])->order('xu asc')->select();
        $this->assign('BannerList',$BannerList);

        return $this->fetch("GoodsInvest3");
    }


    /**
     * 复消商品
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GoodsRepeat(){
        //商品名称
        $GoodsName = Param('goodsname');
        $this->assign('GoodsName',$GoodsName);

        if(!empty($GoodsName)){
            $Where['name'] = array('like','%'.$GoodsName.'%');
        }

        $Where['is_shelf'] = 2;
        $Where['is_invest'] = 2;
        // $GoodsList = Db::name('goods')->where($Where)->order('add_time desc')->select();
        $GoodsList = Db::name('goods')->where($Where)->order('xu asc')->select();
        $this->assign('GoodsList',$GoodsList);

        //轮播图
        $BannerList = Db::name('banner')->where(['place'=>4])->order('xu asc')->select();
        $this->assign('BannerList',$BannerList);

        return $this->fetch("GoodsRepeat");
    }

    /**
     * 商品详情
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GoodsDetail(){
        //商品ID
        $GoodsID = Param('goods_id');
        $UserInfo = UserInfo(['id'=>Session::get('user_id')]);
        $this->assign('UserInfo',$UserInfo);
        //商品信息
        $GoodsInfo = GoodsInfo(['id'=>$GoodsID]);
//         var_dump($GoodsInfo['is_sheng']);
//         var_dump($UserInfo['level']);die;
        $GoodsInfo['album'] = explode(",",$GoodsInfo['album']);
        $this->assign('GoodsInfo',$GoodsInfo);
        //同大类产品
        $GoodsList = Db::name('goods')->where(['parent_id'=>$GoodsInfo['parent_id'],'is_shelf'=>2])->select();
        $this->assign('GoodsList',$GoodsList);
        //倒计时
        if($GoodsInfo['etime'] == ''){
            $time = 0;
        }else{
            // $time = date('Y-m-d H:i:s',$GoodsInfo['etime']);
            $time = $GoodsInfo['etime'];
        }
        $this->assign('time',$time);
        $nowtime = time();
        $this->assign('nowtime',$nowtime);

        //系统参数
        $Config = Config();
        $this->assign("Config",$Config);

        $appid = $Config['wechat_appid'];
        $appsecret = $Config['wechat_appsecret'];
//        dump($appid);
//        dump($appsecret);die;
        $jssdk = new JSSDK($appid, $appsecret);
//
        $signPackage = $jssdk->GetSignPackage();
//        var_dump($signPackage);die;
        $this->assign('signPackage',$signPackage);
        return $this->fetch('GoodsDetail');
    }

}


class JSSDK {
    private $appId;
    private $appSecret;
    public function __construct($appId, $appSecret) {
        $this->appId = $appId;
        $this->appSecret = $appSecret;
    }
    public function getSignPackage() {
        $jsapiTicket = $this->getJsApiTicket();
        $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $timestamp = time();
        $nonceStr = $this->createNonceStr();
        // 这里参数的顺序要按照 key 值 ASCII 码升序排序
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
        // $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&tamp=$timestamp&url=$url";
        $signature = sha1($string);
        $signPackage = array(
            "appId"     => $this->appId,
            "nonceStr"  => $nonceStr,
            "timestamp" => $timestamp,
            "url"       => $url,
            "signature" => $signature,
            "rawString" => $string
        );
        return $signPackage;
    }
    private function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
    private function getJsApiTicket() {
        // jsapi_ticket 应该全局存储与更新，以下代码以写入到文件中做示例
        $data = json_decode(file_get_contents("jsapi_ticket.json"));
        if ($data->expire_time < time()) {
            $accessToken = $this->getAccessToken();
            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
            $res = json_decode($this->httpGet($url));
            $ticket = $res->ticket;
            if ($ticket) {
                $data->expire_time = time() + 7000;
                $data->jsapi_ticket = $ticket;
                $fp = fopen("jsapi_ticket.json", "w");
                fwrite($fp, json_encode($data));
                fclose($fp);
            }
        } else {
            $ticket = $data->jsapi_ticket;
        }
        return $ticket;
    }
    private function getAccessToken() {
        // access_token 应该全局存储与更新，以下代码以写入到文件中做示例
        $data = json_decode(file_get_contents("access_token.json"));
        if ($data->expire_time < time()) {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
            $res = json_decode($this->httpGet($url));
//            var_dump($res);die;
            $access_token = $res->access_token;
            if ($access_token) {
                $data->expire_time = time() + 7000;
                $data->access_token = $access_token;
                $fp = fopen("access_token.json", "w");
                fwrite($fp, json_encode($data));
                fclose($fp);
            }
        } else {
            $access_token = $data->access_token;
        }
        return $access_token;
    }
    private function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }


}